
#define RANDOMMM 4       //点击随机时间幅度，3~10

float p1 = 7.0;         //点击频率1，
float p2 = 8.2;         //点击频率2，

//长按随机时间最小值和最大值，50~150
int minTTime0 = 65;
int maxTTime0 = 90;

int minTTime1 = 65;
int maxTTime1 = 85;

#define MAXFQUNUMBER 2    
float fquList[MAXFQUNUMBER] = {p1,p2};   
int TTtime[MAXFQUNUMBER][2] = {{minTTime0,maxTTime0},{minTTime1,maxTTime1}};

#define pinnumber 4
#define vpnumber  9
const int tPin[pinnumber][2] = {{2,3},{4,5},{6,7},{8,9}};  //1号引脚定义,灰的在前，白的在后,电阻接白的
const int tvpin[vpnumber] = {0x08,0x04,0x02,0x01,0x0c,0x06,0x03,0x09,0x0f};
/*

  1     2

  4     3

  1:(1),2:(2),3:(3),4:(4),5:(1,2),6:(2,3),7:(3,4),8:(1,4),9:(1,2,3,4)

*/





void cleanAllTouch(){
  for(int i = 0;i<pinnumber;i++){
    digitalWrite(tPin[i][1], LOW);
    digitalWrite(tPin[i][0], HIGH);
  }
}

void touchPinHex(int p){
  if(p&0x08){
    digitalWrite(tPin[0][1], HIGH);
    digitalWrite(tPin[0][0], LOW);  
  }
  if(p&0x04){
    digitalWrite(tPin[1][1], HIGH);
    digitalWrite(tPin[1][0], LOW);  
  }
  if(p&0x02){
    digitalWrite(tPin[2][1], HIGH);
    digitalWrite(tPin[2][0], LOW);  
  }
  if(p&0x01){
    digitalWrite(tPin[3][1], HIGH);
    digitalWrite(tPin[3][0], LOW);  
  }
}

void unTouchPinHex(int p){
  if(p&0x08){
    digitalWrite(tPin[0][0], HIGH);
    digitalWrite(tPin[0][1], LOW);  
  }
  if(p&0x04){
    digitalWrite(tPin[1][0], HIGH);
    digitalWrite(tPin[1][1], LOW);  
  }
  if(p&0x02){
    digitalWrite(tPin[2][0], HIGH);
    digitalWrite(tPin[2][1], LOW);  
  }
  if(p&0x01){
    digitalWrite(tPin[3][0], HIGH);
    digitalWrite(tPin[3][1], LOW);  
  }
}

void touchPin(int p){
  if(p >=0 && p <vpnumber){
    touchPinHex(tvpin[p]);
  }
}
void unTouchPin(int p){
    
if(p >=0 && p <vpnumber){
    unTouchPinHex(tvpin[p]);
  }
}
const int blinkdelay = 50;

void ledBlink(){
  for(int i = 0;i<5;i++){
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(blinkdelay);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(blinkdelay); 
  }
}

#define KeyPin  11     //按键开关引脚定义，红色
#define FkeyPin A5     //频率切换按键，白色


void setup() {
  //初始化串口
  Serial.begin(115200);

  //设置随机数种子
  randomSeed(analogRead(A0));
  pinMode(KeyPin, INPUT_PULLUP); //设置按键引脚为输入
  pinMode(FkeyPin, INPUT); //设置按键引脚为输入

  pinMode(LED_BUILTIN, OUTPUT);
  //设置点击头控制引脚为输出
  for(int i = 0;i<pinnumber;i++){
    pinMode(tPin[i][0], OUTPUT);
    pinMode(tPin[i][1], OUTPUT);
  }
  delay(1);
  
  //初始化点击头状态,清除所有当关点击状态
  cleanAllTouch();
  digitalWrite(LED_BUILTIN, LOW);
}

#define MAXFQUNUMBER 2

int fquIndex = 0;                          //当前点击频率编号
int minTime = int(500/fquList[fquIndex]) - RANDOMMM;  //最小随机间隔时间
int maxTime = int(500/fquList[fquIndex]) + RANDOMMM;  //最大随机间隔时间

int ttmindelay = TTtime[fquIndex][0];
int ttmaxdelay = TTtime[fquIndex][1];


//通过频率更新随机间隔时间
void updateRandomTime(int index){
  float f = fquList[index];
  minTime = int(500/f) - RANDOMMM;  //最小随机间隔时间
  maxTime = int(500/f) + RANDOMMM;  //最大随机间隔时间
  ttmindelay = TTtime[index][0];
  ttmaxdelay = TTtime[index][1];
}
int ttime = 125;
int utime = 125;
void loop() {
  int k = digitalRead(KeyPin);
  if(!k){
    int p = random(0,vpnumber);               //取随机按键
    ttime = random(ttmindelay,ttmaxdelay);   //取随机时间
    touchPin(p);
    delay(ttime);
    utime = random(minTime,maxTime);   //取随机时间
    unTouchPin(p);
    delay(utime);
  }else{
    int fk = digitalRead(FkeyPin);
    if(!fk){
        fquIndex++;
        if(fquIndex >= MAXFQUNUMBER){
          fquIndex = 0;
          updateRandomTime(fquIndex);
        }else{
          updateRandomTime(fquIndex);
        }
        ledBlink();
    }
  }
}
